﻿using AyaWo.Commen;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AyaWo.DAL
{
    /// <summary>
    /// 存储内容方法
    /// </summary>
   public class spcontentService:BaseService
    {
        /// <summary>
        /// 添加内容
        /// </summary>
        /// <param name="obj"></param>
        public static void AddContent(spidercontent obj)
        {
            new spcontentService().InsertContents(obj);
        }

        /// <summary>
        /// 添加内容
        /// </summary>
        /// <param name="obj"></param>
        private void InsertContents(spidercontent obj)
        {
            try
            {
                string sqlstr = "INSERT into spidercontent(ID,Link,Title,BuildTime,Source,Autos,Content,CreateTime,HasTonbu,typ,CountType) VALUES(@ID,@Link,@Title,@BuildTime,@Source,@Autos,@Content,@CreateTime,@HasTonbu,@typ,@CountType)";
                Db.ExecuteBySql(new StringBuilder(sqlstr), obj);
            }
            catch ( Exception ex)
            {
                var r = ex;
            }
        }

        /// <summary>
        /// 判断抓取的数据连接和标题是否之前已经存在数据库中了
        /// </summary>
        /// <param name="Link"></param>
        /// <returns></returns>
        public static bool CanInsert( string link)
        {
            return new spcontentService().CanInserts(link);
        }

        public bool CanInserts(string link)
        {
            try
            {
                var q = Db.GetDataTableBySqL(new StringBuilder("select ID from spidercontent t where t.Link like @Link AND typ='1' "), new { Link = "%" + link + "%" });
                if (q == null || q.Rows.Count == 0)
                    return true;
                else return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool CanInserts(string title,int? typ)
        {
            try
            {
                var q = Db.GetDataTableBySqL(new StringBuilder("select ID from spidercontent t where t.title like @title AND typ=@typ "), new { title = "%" + title + "%", typ= typ });
                if (q == null || q.Rows.Count == 0)
                    return true;
                else return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool LinkCanInserts(string link, int? typ)
        {
            try
            {
                var q = Db.GetDataTableBySqL(new StringBuilder("select ID from spidercontent t where t.Link like @Link AND typ=@typ "), new { Link = "%" + link + "%", typ = typ });
                if (q == null || q.Rows.Count == 0)
                    return true;
                else return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 获取在还没有被关键字搜过过的  且 关键字有设置过的数据列表  
        /// </summary>
        /// <returns></returns>
        //public List<spidercontent> GetList()
        //{
        //    string sqlStr = "select * from spidercontent t ORDER BY t.CreateTime desc";
        //    return Db.GetDataListBySqL<spidercontent>(new StringBuilder(sqlStr));
        //}

        public static DataTable GetList()
        {
            string sqlStr = "select ID,Link,Title,BuildTime,Source,Autos,CreateTime,HasTonbu,typ,CountType from spidercontent ORDER BY CreateTime desc";
            return Db.GetDataTableBySqL(new StringBuilder(sqlStr));
        }

        public static List<spidercontent> GetLists()
        {
            return Db.GetList<spidercontent>(null).ToList();
            //string sqlStr = "select ID,Title,BuildTime,Source,Autos,CreateTime,HasTonbu,typ,CountType from spidercontent ORDER BY CreateTime desc";
            //return Db.GetDataListBySqL<spidercontent>(new StringBuilder(sqlStr));
        }

        public static DataTable GetSqliteList()
        {
            string sqlStr = "select ID 编码,Link 网站地址,Title 标题,BuildTime 创建时间,Source 来源,Autos 作者,CreateTime 采集时间,HasTonbu 是否同步,typ 类别,CountType 文章分类 from spidercontent ORDER BY CreateTime desc";
            return Db.GetDataTableBySqL(new StringBuilder(sqlStr));
        }

        public static int UpdateSet(string kenID)
        {
            string sqlStr = "UPDATE spidercontent SET HasTonbu=1 WHERE ID=@ID";
            return Db.ExecuteBySql(new StringBuilder(sqlStr), new { ID = kenID });
        }
    }
}
